package com.baoyouqun.domain.VO;

import com.alibaba.fastjson2.JSONArray;
import com.baoyouqun.domain.Enum.SystemConfigType;
import com.baoyouqun.entity.SystemConfig;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Data
public class VipMemberDTO implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    private String id;

    @ApiModelProperty(value = "VIP会员价格配置")
    private BigDecimal price;

    @ApiModelProperty(value = "权益列表")
    private List<VipMemberIconDTO> list;

    /**
     * 从SystemConfig转换（使用FastJSON2反序列化JSON数组）
     */
    public static VipMemberDTO fromSystemConfig(SystemConfig systemConfig) {
        if (systemConfig == null || !SystemConfigType.VIP_MEMBER.equals(systemConfig.getConfigType())) {
            throw new IllegalArgumentException("无效的VIP会员配置数据");
        }

        VipMemberDTO dto = new VipMemberDTO();
        dto.setId(systemConfig.getId());
        // BigDecimal类型映射
        dto.setPrice(systemConfig.getInfo5()); // info5是BigDecimal

        // 从info8读取JSON数组并反序列化为List<VipMemberIconDTO>
        String listJson = systemConfig.getInfo8();
        if (listJson != null && !listJson.trim().isEmpty()) {
            try {
                // 去除首尾可能存在的双引号（处理格式异常）
                listJson = listJson.trim();
                if (listJson.startsWith("\"") && listJson.endsWith("\"")) {
                    listJson = listJson.substring(1, listJson.length() - 1);
                }
                // 去除引号后再解析
                JSONArray jsonArray = JSONArray.parseArray(listJson);
                List<VipMemberIconDTO> dtoList = jsonArray.toJavaList(VipMemberIconDTO.class);
                dto.setList(dtoList);
            } catch (Exception e) {
                throw new RuntimeException("VIP权益列表反序列化失败: " + listJson, e);
            }
        } else {
            dto.setList(new ArrayList<>()); // 空列表默认值
        }

        return dto;
    }

    /**
     * 转换为SystemConfig（使用FastJSON2序列化List为JSON数组）
     */
    public SystemConfig toSystemConfig(SystemConfig config) {
        config.setConfigType(SystemConfigType.VIP_MEMBER);
        config.setConfigName(SystemConfigType.VIP_MEMBER.getDesc());

        // BigDecimal类型映射
        config.setInfo5(this.price); // VIP价格（BigDecimal → info5）

        // 使用FastJSON2将List序列化为JSON数组字符串，存入info8
        try {
            if (this.list != null) {
                JSONArray jsonArray = new JSONArray();
                jsonArray.addAll(this.list);
                config.setInfo8(jsonArray.toJSONString());
            } else {
                config.setInfo8("[]"); // 空数组默认值
            }
        } catch (Exception e) {
            throw new RuntimeException("VIP权益列表序列化失败", e);
        }

        return config;
    }

    @Data
    public static class VipMemberIconDTO implements Serializable {
        private static final long serialVersionUID = 1L;

        @ApiModelProperty(value = "图标URL")
        private String iconUrl;

        @ApiModelProperty(value = "权益标题")
        private String title;

        @ApiModelProperty(value = "权益描述")
        private String content;
    }
}
